package org.loofer.ext.utils;

import android.content.Context;
import android.content.res.AssetManager;
import org.loofer.ext.Ext;
import org.loofer.ext.component.logger.Logger;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public final class FileUtils {
    private static final int ASSET_SPLIT_BASE = 0;
    private static final int BUFFER_SIZE = 8192;
    private static final String LOG_TAG = "FileUtils";
    public static final AssetFileComparator SIMPLE_ASSET_COMPARATOR = FileUtils$$Lambda$3.lambdaFactory$();
    public static final FileComparator SIMPLE_COMPARATOR = FileUtils$$Lambda$1.lambdaFactory$();
    public static final FileComparator STRICT_COMPARATOR = FileUtils$$Lambda$2.lambdaFactory$();
    private static final String TAG = "FileUtils";
    private static String mSeparator = File.separator;
    private static char mSeparatorChar = File.separatorChar;

    public interface FileComparator {
        boolean equals(File file, File file2);
    }

    public interface AssetFileComparator {
        boolean equals(Context context, String str, File file);
    }

    private static /* synthetic */ boolean lambda$static$0(File lhs, File rhs) {
        return lhs.length() == rhs.length() && lhs.lastModified() == rhs.lastModified();
    }

    private static /* synthetic */ boolean lambda$static$1(File lhs, File rhs) {
        String lhsMd5 = SecurityUtils.digest(lhs);
        if (lhsMd5 == null) {
            return false;
        }
        return lhsMd5.equals(SecurityUtils.digest(rhs));
    }

    private static /* synthetic */ boolean lambda$static$2(Context context, String assetPath, File dstFile) {
        long assetFileLength = getAssetLength(context, assetPath);
        return assetFileLength != -1 && assetFileLength == dstFile.length();
    }

    private FileUtils() {
    }

    public static boolean copyFiles(File src, File dst) {
        return copyFiles(src, dst, null);
    }

    public static boolean copyFiles(File src, File dst, FileFilter filter) {
        return copyFiles(src, dst, filter, SIMPLE_COMPARATOR);
    }

    public static boolean copyFiles(File src, File dst, FileFilter filter, FileComparator comparator) {
        int i = 0;
        if (src == null || dst == null) {
            return false;
        }
        if (!src.exists()) {
            return false;
        }
        if (src.getAbsolutePath().equals(dst.getAbsolutePath())) {
            return true;
        }
        if (src.isFile()) {
            return performCopyFile(src, dst, filter, comparator);
        }
        File[] paths = src.listFiles();
        if (paths == null) {
            return false;
        }
        boolean result = true;
        int length = paths.length;
        while (i < length) {
            File sub = paths[i];
            if (!copyFiles(sub, new File(dst, sub.getName()), filter)) {
                result = false;
            }
            i++;
        }
        return result;
    }

    private static boolean performCopyFile(File srcFile, File dstFile, FileFilter filter, FileComparator comparator) {
        if (filter != null && !filter.accept(srcFile)) {
            return true;
        }
        Closeable inc = null;
        Closeable ouc = null;
        if (dstFile.exists()) {
            if (comparator != null && comparator.equals(srcFile, dstFile)) {
                return true;
            }
            try {
                delete(dstFile);
            } catch (Throwable e) {
                Logger.i("FileUtils", "fail to copy file", e);
                delete(dstFile);
                return false;
            } finally {
                IoUtils.closeSilently(inc);
                IoUtils.closeSilently(ouc);
            }
        }
        File toParent = dstFile.getParentFile();
        if (toParent.isFile()) {
            delete(toParent);
        }
        if (toParent.exists() || toParent.mkdirs()) {
            inc = new FileInputStream(srcFile).getChannel();
            ouc = new FileOutputStream(dstFile).getChannel();
            ouc.transferFrom(inc, 0, inc.size());
            IoUtils.closeSilently(inc);
            IoUtils.closeSilently(ouc);
            return true;
        }
        IoUtils.closeSilently(null);
        IoUtils.closeSilently(null);
        return false;
    }

    public static boolean copyFile(InputStream source, File dst) {
        return copyFile(source, dst, false);
    }

    public static boolean copyFile(InputStream source, File dst, boolean closeWhenFinish) {
        Throwable e;
        Throwable th;
        boolean z = false;
        if (!(source == null || dst == null)) {
            Closeable ous = null;
            try {
                Closeable ous2 = new BufferedOutputStream(new FileOutputStream(dst), 8192);
                try {
                    z = performCopyStream(source, ous2);
                    if (closeWhenFinish) {
                        IoUtils.closeSilently((Closeable) source);
                    }
                    IoUtils.closeSilently(ous2);
                } catch (Throwable th2) {
                    th = th2;
                    ous = ous2;
                    if (closeWhenFinish) {
                        IoUtils.closeSilently((Closeable) source);
                    }
                    IoUtils.closeSilently(ous);
                    throw th;
                }
            } catch (Throwable th3) {
                e = th3;
                Logger.i("FileUtils", "fail to copy file", e);
                if (closeWhenFinish) {
                    IoUtils.closeSilently((Closeable) source);
                }
                IoUtils.closeSilently(ous);
                return z;
            }
        }
        return z;
    }

    private static boolean performCopyStream(InputStream ins, OutputStream ous) {
        try {
            byte[] buffer = new byte[8192];
            while (true) {
                int count = ins.read(buffer);
                if (count <= 0) {
                    return true;
                }
                ous.write(buffer, 0, count);
            }
        } catch (Throwable e) {
            Logger.i("FileUtils", "fail to copy stream", e);
            return false;
        }
    }

    public static boolean moveFiles(File src, File dst) {
        return moveFiles(src, dst, null);
    }

    public static boolean moveFiles(File src, File dst, FileFilter filter) {
        boolean sameStorage = true;
        if (src == null || dst == null || !src.exists()) {
            return false;
        }
        if (src.getAbsolutePath().equals(dst.getAbsolutePath())) {
            return true;
        }
        if (StorageUtils.isInternal(src.getAbsolutePath()) != StorageUtils.isInternal(dst.getAbsolutePath())) {
            sameStorage = false;
        }
        return performMoveFiles(src, dst, filter, sameStorage);
    }

    private static boolean performMoveFiles(File src, File dst, FileFilter filter, boolean rename) {
        int i = 0;
        boolean result;
        if (src.isFile()) {
            result = false;
            if (rename) {
                result = performRenameFile(src, dst, filter);
            }
            if (result) {
                return result;
            }
            result = performCopyFile(src, dst, filter, null);
            delete(src);
            return result;
        }
        File[] paths = src.listFiles();
        if (paths == null) {
            return false;
        }
        result = true;
        int length = paths.length;
        while (i < length) {
            File sub = paths[i];
            if (!performMoveFiles(sub, new File(dst, sub.getName()), filter, rename)) {
                result = false;
            }
            i++;
        }
        return result;
    }

    private static boolean performRenameFile(File srcFile, File dstFile, FileFilter filter) {
        if (filter != null && !filter.accept(srcFile)) {
            return true;
        }
        if (dstFile.exists()) {
            delete(dstFile);
        }
        File toParent = dstFile.getParentFile();
        if (toParent.isFile()) {
            delete(toParent);
        }
        if (toParent.exists() || toParent.mkdirs()) {
            return srcFile.renameTo(dstFile);
        }
        return false;
    }

    public static boolean copyAssets(Context context, String assetName, String dst) {
        return copyAssets(context, assetName, dst, SIMPLE_ASSET_COMPARATOR);
    }

    public static boolean copyAssets(Context context, String assetName, String dst, AssetFileComparator comparator) {
        return performCopyAssetsFile(context, assetName, dst, comparator);
    }

    private static boolean performCopyAssetsFile(Context context, String assetPath, String dstPath, AssetFileComparator comparator) {
        Throwable e;
        Throwable th;
        if (isEmpty(assetPath) || isEmpty(dstPath)) {
            return false;
        }
        AssetManager assetManager = context.getAssets();
        File dstFile = new File(dstPath);
        Closeable in = null;
        Closeable closeable = null;
        if (dstFile.exists()) {
            if (comparator == null || !comparator.equals(context, assetPath, dstFile)) {
                try {
                    if (dstFile.isDirectory()) {
                        delete(dstFile);
                    }
                } catch (Throwable th2) {
                    e = th2;
                    try {
                        Logger.i("FileUtils", "fail to copy assets file", e);
                        delete(dstFile);
                        IoUtils.closeSilently(in);
                        IoUtils.closeSilently(closeable);
                        return false;
                    } catch (Throwable th3) {
                        th = th3;
                        IoUtils.closeSilently(in);
                        IoUtils.closeSilently(closeable);
                        throw th;
                    }
                }
            }
            IoUtils.closeSilently(null);
            IoUtils.closeSilently(null);
            return true;
        }
        File parent = dstFile.getParentFile();
        if (parent.isFile()) {
            delete(parent);
        }
        if (parent.exists() || parent.mkdirs()) {
            in = assetManager.open(assetPath);
            Closeable out = new BufferedOutputStream(new FileOutputStream(dstFile), 8192);
            try {
                byte[] buf = new byte[8192];
                while (true) {
                    int len = in.read(buf);
                    if (len > 0) {
                        out.write(buf, 0, len);
                    } else {
                        IoUtils.closeSilently(in);
                        IoUtils.closeSilently(out);
                        closeable = out;
                        return true;
                    }
                }
            } catch (Throwable th4) {
                th = th4;
                closeable = out;
                IoUtils.closeSilently(in);
                IoUtils.closeSilently(closeable);
                throw th;
            }
        }
        IoUtils.closeSilently(null);
        IoUtils.closeSilently(null);
        return false;
    }

    public static long getAssetLength(Context context, String assetPath) {
        AssetManager assetManager = context.getAssets();
        try {
            return assetManager.openFd(assetPath).getLength();
        } catch (IOException e) {
            Closeable closeable = null;
            try {
                closeable = assetManager.open(assetPath);
                long available = (long) closeable.available();
                return available;
            } catch (IOException e2) {
                return -1;
            } finally {
                IoUtils.closeSilently(closeable);
            }
        }
    }

    public static boolean delete(File file) {
        return delete(file, false);
    }

    public static boolean delete(File file, boolean ignoreDir) {
        if (file == null || !file.exists()) {
            return false;
        }
        if (file.isFile()) {
            return file.delete();
        }
        File[] fileList = file.listFiles();
        if (fileList == null) {
            return false;
        }
        for (File f : fileList) {
            delete(f, ignoreDir);
        }
        if (ignoreDir || !file.delete()) {
            return false;
        }
        return true;
    }

    public static boolean delete(File file, FileFilter filter) {
        if (file == null || !file.exists()) {
            return false;
        }
        if (!file.isFile()) {
            File[] fileList = file.listFiles();
            if (fileList == null) {
                return false;
            }
            for (File f : fileList) {
                delete(f, filter);
            }
            if ((filter == null || filter.accept(file)) && file.delete()) {
                return true;
            }
            return false;
        } else if ((filter == null || filter.accept(file)) && file.delete()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean mkdirs(File dir) {
        if (dir.exists()) {
            if (dir.isDirectory()) {
                return true;
            }
            delete(dir);
        }
        return dir.mkdirs();
    }

    private static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static synchronized void save(String filename, String content) throws Exception {
        synchronized (FileUtils.class) {
            FileOutputStream outStream = new FileOutputStream(Ext.getContext().getFileStreamPath(filename));
            outStream.write(content.getBytes());
            outStream.close();
        }
    }

    public static String readFile(String filename) {
        byte[] data = null;
        try {
            FileInputStream inStream = new FileInputStream(Ext.getContext().getFileStreamPath(filename));
            byte[] buffer = new byte[4096];
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            int temp1 = inStream.read(buffer);
            if (temp1 == -1) {
                Logger.v("FileUtils", "DoReadFileFuncuBufferNull: ");
                return null;
            }
            outStream.write(buffer, 0, temp1);
            while (true) {
                int len = inStream.read(buffer);
                if (len == -1) {
                    break;
                }
                outStream.write(buffer, 0, len);
            }
            data = outStream.toByteArray();
            outStream.close();
            inStream.close();
            return new String(data);
        } catch (Exception e) {
            Logger.v("FileUtils", "ERROR_DoReadFileFunc: ", e);
            e.printStackTrace();
        }
    }

    public static void setSeparatorChar(char separatorChar) {
        mSeparatorChar = separatorChar;
        mSeparator = String.valueOf(mSeparatorChar);
    }

    public static boolean exists(String path) {
        return !StringUtils.isEmpty(path) && new File(path).exists();
    }

    public static boolean fileExists(String path) {
        if (StringUtils.isEmpty(path)) {
            return false;
        }
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            return true;
        }
        return false;
    }

    public static boolean updateFileLastModified(String filePath, long lastModified) {
        return fileExists(filePath) && new File(filePath).setLastModified(lastModified);
    }

    public static long fileLength(String path) {
        if (fileExists(path)) {
            return new File(path).length();
        }
        return 0;
    }

    public static boolean folderExists(String path) {
        if (StringUtils.isEmpty(path)) {
            return false;
        }
        File file = new File(path);
        if (file.exists() && file.isDirectory()) {
            return true;
        }
        return false;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static synchronized File createFile(String r6) {
        /*
        r3 = 0;
        r4 = org.loofer.ext.utils.FileUtils.class;
        monitor-enter(r4);
        r5 = org.loofer.ext.utils.StringUtils.isEmpty(r6);	 Catch:{ all -> 0x0037 }
        if (r5 == 0) goto L_0x000d;
    L_0x000a:
        r1 = r3;
    L_0x000b:
        monitor-exit(r4);
        return r1;
    L_0x000d:
        r1 = new java.io.File;	 Catch:{ all -> 0x0037 }
        r1.<init>(r6);	 Catch:{ all -> 0x0037 }
        r5 = r1.isFile();	 Catch:{ all -> 0x0037 }
        if (r5 != 0) goto L_0x000b;
    L_0x0018:
        r2 = r1.getParentFile();	 Catch:{ all -> 0x0037 }
        if (r2 == 0) goto L_0x0030;
    L_0x001e:
        r5 = r2.isDirectory();	 Catch:{ all -> 0x0037 }
        if (r5 != 0) goto L_0x002a;
    L_0x0024:
        r5 = r2.mkdirs();	 Catch:{ all -> 0x0037 }
        if (r5 == 0) goto L_0x0030;
    L_0x002a:
        r5 = r1.createNewFile();	 Catch:{ Exception -> 0x0032 }
        if (r5 != 0) goto L_0x000b;
    L_0x0030:
        r1 = r3;
        goto L_0x000b;
    L_0x0032:
        r0 = move-exception;
        r0.printStackTrace();	 Catch:{ all -> 0x0037 }
        goto L_0x0030;
    L_0x0037:
        r3 = move-exception;
        monitor-exit(r4);
        throw r3;
        */
        throw new UnsupportedOperationException("Method not decompiled: org.loofer.ext.utils.FileUtils.createFile(java.lang.String):java.io.File");
    }

    public static synchronized File createFolder(String path) {
        File file = null;
        synchronized (FileUtils.class) {
            if (!StringUtils.isEmpty(path)) {
                File file2 = new File(path);
                if (!(file2.isDirectory() || file2.mkdirs())) {
                    file2 = null;
                }
                file = file2;
            }
        }
        return file;
    }

    public static synchronized long getFileSize(String path) {
        long j;
        synchronized (FileUtils.class) {
            if (StringUtils.isEmpty(path)) {
                j = 0;
            } else {
                j = getFileSize(new File(path));
            }
        }
        return j;
    }

    public static synchronized long getFileSize(File file) {
        long j;
        synchronized (FileUtils.class) {
            long size = 0;
            if (file == null) {
                j = 0;
            } else {
                if (file.isDirectory()) {
                    File[] files = file.listFiles();
                    if (files != null) {
                        for (File f : files) {
                            size += f.isDirectory() ? getFileSize(f) : f.length();
                        }
                    }
                } else {
                    size = file.length();
                }
                j = size;
            }
        }
        return j;
    }

    public static synchronized int clearFolder(String path, long interval) {
        int i;
        synchronized (FileUtils.class) {
            if (StringUtils.isEmpty(path)) {
                i = 0;
            } else {
                i = clearFolder(new File(path), interval);
            }
        }
        return i;
    }

    public static synchronized void clearFolder(String path, long maxRemainSize, boolean distinct) {
        synchronized (FileUtils.class) {
            clearFolder(path, maxRemainSize, null);
        }
    }

    public static synchronized void clearFolder(String path, long maxRemainSize, String[] except) {
        synchronized (FileUtils.class) {
            long size = getFileSize(path);
            if (size > maxRemainSize) {
                List<String> exceptFilePathList = except == null ? new ArrayList() : Arrays.asList(except);
                File[] files = new File(path).listFiles();
                if (files != null) {
                    List<File> fileList = Arrays.asList(files);
                    try {
                        Collections.sort(fileList, new Comparator<File>() {
                            public int compare(File lhs, File rhs) {
                                if (lhs.lastModified() == rhs.lastModified()) {
                                    return 0;
                                }
                                return lhs.lastModified() < rhs.lastModified() ? -1 : 1;
                            }
                        });
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }
                    for (File file : fileList) {
                        if (size <= maxRemainSize) {
                            break;
                        } else if (!exceptFilePathList.contains(file.getAbsolutePath())) {
                            file.lastModified();
                            size -= file.length();
                            file.delete();
                        }
                    }
                }
            }
        }
    }

    public static synchronized int clearFolder(File path, long interval) {
        int deletedItems;
        synchronized (FileUtils.class) {
            long expiredTimeMillis = System.currentTimeMillis() - interval;
            deletedItems = 0;
            File[] fileList = path.listFiles();
            if (fileList != null) {
                for (File file : fileList) {
                    if (file.isDirectory()) {
                        deletedItems += clearFolder(file, interval);
                    }
                    if (file.lastModified() < expiredTimeMillis && file.delete()) {
                        deletedItems++;
                    }
                }
            }
        }
        return deletedItems;
    }

    public static int clearFolderNoSynchronized(File path, long interval) {
        long expiredTimeMillis = System.currentTimeMillis() - interval;
        int deletedFiles = 0;
        File[] fileList = path.listFiles();
        if (fileList != null) {
            for (File file : fileList) {
                if (file.isDirectory()) {
                    deletedFiles += clearFolder(file, interval);
                }
                if (file.lastModified() < expiredTimeMillis && file.delete()) {
                    deletedFiles++;
                }
            }
        }
        return deletedFiles;
    }

    public static synchronized int clearFolder(File path) {
        int deletedItems;
        synchronized (FileUtils.class) {
            deletedItems = 0;
            File[] fileList = path.listFiles();
            if (fileList != null) {
                for (File file : fileList) {
                    if (file.isDirectory()) {
                        deletedItems += clearFolder(file);
                    }
                    if (file.delete()) {
                        deletedItems++;
                    }
                }
            }
        }
        return deletedItems;
    }

    public static boolean deleteNoSynchronized(String path) {
        return !StringUtils.isEmpty(path) && deleteNoSynchronized(new File(path));
    }

    public static boolean deleteNoSynchronized(File path) {
        if (path == null) {
            return true;
        }
        if (path.isDirectory()) {
            File[] files = path.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (!delete(file)) {
                        return false;
                    }
                }
            }
        }
        if (!path.exists() || path.delete()) {
            return true;
        }
        return false;
    }

    public static synchronized boolean delete(String filePath) {
        boolean z;
        synchronized (FileUtils.class) {
            z = !StringUtils.isEmpty(filePath) && delete(new File(filePath));
        }
        return z;
    }

    public static String load(String path) {
        if (path == null) {
            throw new NullPointerException("path should not be null.");
        }
        String string = null;
        try {
            string = StringUtils.stringFromInputStream(new FileInputStream(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return string != null ? string : "";
    }

    public static synchronized boolean store(String content, String path) {
        IOException e;
        Throwable th;
        ArrayIndexOutOfBoundsException e2;
        boolean z = false;
        synchronized (FileUtils.class) {
            if (path == null) {
                throw new NullPointerException("path should not be null.");
            }
            BufferedWriter bufferedWriter = null;
            try {
                File file = createFile(path);
                if (file == null) {
                    Logger.d("FileUtils", "file == null path=" + path);
                    if (bufferedWriter != null) {
                        try {
                            bufferedWriter.close();
                        } catch (IOException e3) {
                            e3.printStackTrace();
                        }
                    }
                } else {
                    BufferedWriter bufferedWriter2 = new BufferedWriter(new FileWriter(file));
                    if (content == null) {
                        content = "";
                    }
                    try {
                        bufferedWriter2.write(content);
                        bufferedWriter2.flush();
                        z = true;
                        if (bufferedWriter2 != null) {
                            try {
                                bufferedWriter2.close();
                            } catch (IOException e32) {
                                e32.printStackTrace();
                            }
                        }
                        bufferedWriter = bufferedWriter2;
                    } catch (IOException e4) {
                        e32 = e4;
                        bufferedWriter = bufferedWriter2;
                        try {
                            e32.printStackTrace();
                            if (bufferedWriter != null) {
                                try {
                                    bufferedWriter.close();
                                } catch (IOException e322) {
                                    e322.printStackTrace();
                                }
                            }
                            return z;
                        } catch (Throwable th2) {
                            th = th2;
                            if (bufferedWriter != null) {
                                try {
                                    bufferedWriter.close();
                                } catch (IOException e3222) {
                                    e3222.printStackTrace();
                                }
                            }
                            throw th;
                        }
                    } catch (ArrayIndexOutOfBoundsException e5) {
                        e2 = e5;
                        bufferedWriter = bufferedWriter2;
                        e2.printStackTrace();
                        if (bufferedWriter != null) {
                            try {
                                bufferedWriter.close();
                            } catch (IOException e32222) {
                                e32222.printStackTrace();
                            }
                        }
                        return z;
                    } catch (Throwable th3) {
                        th = th3;
                        bufferedWriter = bufferedWriter2;
                        if (bufferedWriter != null) {
                            bufferedWriter.close();
                        }
                        throw th;
                    }
                }
            } catch (IOException e6) {
                e32222 = e6;
                e32222.printStackTrace();
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
                return z;
            } catch (ArrayIndexOutOfBoundsException e7) {
                e2 = e7;
                e2.printStackTrace();
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
                return z;
            }
        }
        return z;
    }

    public static synchronized boolean store(InputStream inputStream, String path) {
        Exception e;
        Throwable th;
        boolean z = false;
        synchronized (FileUtils.class) {
            if (path == null) {
                throw new NullPointerException("path should not be null.");
            }
            FileOutputStream fileOutputStream = null;
            try {
                File file = createFile(path);
                if (file == null) {
                    Logger.d("FileUtils", "inputStream file == null path=" + path);
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                    }
                    try {
                        inputStream.close();
                    } catch (Exception e3) {
                        e3.printStackTrace();
                    }
                } else {
                    byte[] buffer = new byte[8192];
                    FileOutputStream fileOutputStream2 = new FileOutputStream(file);
                    while (true) {
                        try {
                            int length = inputStream.read(buffer);
                            if (length <= 0) {
                                break;
                            }
                            fileOutputStream2.write(buffer, 0, length);
                        } catch (Exception e4) {
                            e3 = e4;
                            fileOutputStream = fileOutputStream2;
                        } catch (Throwable th2) {
                            th = th2;
                            fileOutputStream = fileOutputStream2;
                        }
                    }
                    z = true;
                    if (fileOutputStream2 != null) {
                        try {
                            fileOutputStream2.close();
                        } catch (IOException e22) {
                            e22.printStackTrace();
                        }
                    }
                    try {
                        inputStream.close();
                    } catch (Exception e32) {
                        e32.printStackTrace();
                    }
                    fileOutputStream = fileOutputStream2;
                }
            } catch (Exception e5) {
                e32 = e5;
                try {
                    e32.printStackTrace();
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e222) {
                            e222.printStackTrace();
                        }
                    }
                    try {
                        inputStream.close();
                    } catch (Exception e322) {
                        e322.printStackTrace();
                    }
                    return z;
                } catch (Throwable th3) {
                    th = th3;
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e2222) {
                            e2222.printStackTrace();
                        }
                    }
                    try {
                        inputStream.close();
                    } catch (Exception e3222) {
                        e3222.printStackTrace();
                    }
                    throw th;
                }
            }
        }
        return z;
    }

    public static boolean storeNoSynchronized(InputStream inputStream, String filePath) {
        Exception e;
        Throwable th;
        boolean z = false;
        if (filePath == null) {
            throw new NullPointerException("filePath should not be null.");
        }
        FileOutputStream fileOutputStream = null;
        try {
            File storeFile = createFile(filePath);
            if (storeFile == null) {
                Logger.d("FileUtils", "inputStream sotreFile == null filePath=" + filePath);
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                }
                try {
                    inputStream.close();
                } catch (Exception e3) {
                    e3.printStackTrace();
                }
            } else {
                byte[] buffer = new byte[8192];
                FileOutputStream storeFileOutputStream = new FileOutputStream(storeFile);
                while (true) {
                    try {
                        int length = inputStream.read(buffer);
                        if (length <= 0) {
                            break;
                        }
                        storeFileOutputStream.write(buffer, 0, length);
                    } catch (Exception e4) {
                        e3 = e4;
                        fileOutputStream = storeFileOutputStream;
                    } catch (Throwable th2) {
                        th = th2;
                        fileOutputStream = storeFileOutputStream;
                    }
                }
                z = true;
                if (storeFileOutputStream != null) {
                    try {
                        storeFileOutputStream.close();
                    } catch (IOException e22) {
                        e22.printStackTrace();
                    }
                }
                try {
                    inputStream.close();
                } catch (Exception e32) {
                    e32.printStackTrace();
                }
                fileOutputStream = storeFileOutputStream;
            }
        } catch (Exception e5) {
            e32 = e5;
            try {
                e32.printStackTrace();
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e222) {
                        e222.printStackTrace();
                    }
                }
                try {
                    inputStream.close();
                } catch (Exception e322) {
                    e322.printStackTrace();
                }
                return z;
            } catch (Throwable th3) {
                th = th3;
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e2222) {
                        e2222.printStackTrace();
                    }
                }
                try {
                    inputStream.close();
                } catch (Exception e3222) {
                    e3222.printStackTrace();
                }
                throw th;
            }
        }
        return z;
    }

    public static boolean copy(String desPath, String srcPath) {
        FileNotFoundException e;
        if (desPath == null || srcPath == null) {
            throw new NullPointerException("path should not be null.");
        }
        FileInputStream input = null;
        try {
            InputStream input2 = new FileInputStream(srcPath);
            InputStream inputStream;
            try {
                inputStream = input2;
                return store(input2, desPath);
            } catch (FileNotFoundException e2) {
                e = e2;
                inputStream = input2;
                e.printStackTrace();
                try {
                    input.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                return false;
            }
        } catch (FileNotFoundException e3) {
            e = e3;
            e.printStackTrace();
            input.close();
            return false;
        }
    }

    public static String getFileName(String path) {
        if (StringUtils.isEmpty(path)) {
            return "";
        }
        int query = path.lastIndexOf(63);
        if (query > 0) {
            path = path.substring(0, query);
        }
        int filenamePos = path.lastIndexOf(mSeparatorChar);
        return filenamePos >= 0 ? path.substring(filenamePos + 1) : path;
    }

    public static String getFileShortName(String path) {
        String fileName = getFileName(path);
        int separatorIndex = fileName.lastIndexOf(46);
        return separatorIndex > 0 ? fileName.substring(0, separatorIndex) : fileName;
    }

    public static String getFilePath(String path) {
        if (StringUtils.isEmpty(path)) {
            return "";
        }
        int separatorIndex = -1;
        if (path != null && path.startsWith(mSeparator)) {
            separatorIndex = path.lastIndexOf(mSeparatorChar);
        }
        return separatorIndex == -1 ? mSeparator : path.substring(0, separatorIndex);
    }

    public static String getFileExtension(String path) {
        if (!StringUtils.isEmpty(path)) {
            String filename;
            int query = path.lastIndexOf(63);
            if (query > 0) {
                path = path.substring(0, query);
            }
            int filenamePos = path.lastIndexOf(47);
            if (filenamePos >= 0) {
                filename = path.substring(filenamePos + 1);
            } else {
                filename = path;
            }
            if (filename.length() > 0) {
                int dotPos = filename.lastIndexOf(46);
                if (dotPos >= 0) {
                    return filename.substring(dotPos + 1);
                }
            }
        }
        return "";
    }

    public static long lastModified(String path) {
        if (StringUtils.isEmpty(path)) {
            return 0;
        }
        return new File(path).lastModified();
    }

    public static boolean rename(String srcPath, String dstPath) {
        File file = new File(srcPath);
        return file.isFile() && file.renameTo(new File(dstPath));
    }

    public static String validateFileName(String fileName) {
        return fileName == null ? null : fileName.replaceAll("([{/\\\\:*?\"<>|}\\u0000-\\u001f\\uD7B0-\\uFFFF]+)", "");
    }

    public static String getCanonicalPath(String fileName) {
        try {
            fileName = new File(fileName).getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    public static boolean isSamePhysicalPath(String srcPath, String dstPath) {
        Long timeStamp = Long.valueOf(System.currentTimeMillis());
        String srcTmpFilePath = srcPath + File.separator + timeStamp.toString();
        boolean isSame = false;
        createFile(srcTmpFilePath);
        if (exists(srcTmpFilePath) && exists(dstPath + File.separator + timeStamp)) {
            isSame = true;
        }
        delete(srcTmpFilePath);
        return isSame;
    }
}
